home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / powervww / pvapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-05  |  7.3 KB  |  345 lines

  1. //  ____________________________________________________
  2. // |                                                    |
  3. // |  Project:     POWER VIEW INTERFACE                 |
  4. // |  File:        PVAPP.CPP                            |
  5. // |  Compiler:    WPP386 (10.6)                        |
  6. // |                                                    |
  7. // |  Subject:     Power View Application implementation|
  8. // |                                                    |
  9. // |  Author:      Emil Dotchevski                      |
  10. // |____________________________________________________|
  11. //
  12. // E-mail: zajo@geocities.com
  13. // URL:    http://www.geocities.com/SiliconValley/Bay/3577
  14.  
  15. #define uses_app
  16. #define uses_dc
  17. #define uses_icons
  18. #define uses_system
  19.  
  20. #define DECLARE_PVAPP
  21. #include "PVuses.h"
  22. #undef DECLARE_PVAPP
  23.  
  24. static Tcmd_handler _cmd_handler = NULL;
  25. static Tcommands *old_commands = (Tcommands *) MKFP( 0, 1 );
  26.  
  27. static Titem *need_update = NULL;
  28.  
  29. static int cursor_hides = 0;
  30.  
  31. void hide_cursor( void )
  32. {
  33. #ifndef HGR
  34.   if( !cursor_hides++ ) set_cursor( no_cursor );
  35. #endif
  36. }
  37.  
  38. void show_cursor( void )
  39. {
  40.   --cursor_hides;
  41. }
  42.  
  43. void req_update( Titem *p )
  44. {
  45.   Titem *i;
  46.  
  47.   i = need_update;
  48.   while( i != NULL )
  49.   {
  50.     if( i == p ) return;
  51.     i = i->owner;
  52.   }
  53.   need_update = p;
  54. }
  55.  
  56. void cancel_update( Titem *p )
  57. {
  58.   Titem *i;
  59.  
  60.   i = need_update;
  61.   while( i != NULL )
  62.   {
  63.     if( i == p )
  64.     {
  65.       need_update = NULL;
  66.       return;
  67.     }
  68.     i = i->owner;
  69.   }
  70. }
  71.  
  72. static void app_idle( unsigned long time_passed )
  73. {
  74.   if( old_commands != get_cmd() )
  75.   {
  76.     old_commands = get_cmd();
  77.     modal_broadcast( cmAPP_COMMANDS_CHANGED );
  78.   }
  79.   if( need_update != NULL )
  80.   {
  81.     need_update->optimize_bounds();
  82.     need_update = NULL;
  83.   }
  84.   if( !time_passed )
  85.   {
  86.     if( modal_item != NULL ) application->update_cursor();
  87.     application->refresh_screen();
  88.   }
  89. #ifdef MEMMON
  90.   memavail( mem_avail );
  91. #endif
  92. }
  93.  
  94. //Tapplication publics:
  95.  
  96. Tapplication::Tapplication( void ):
  97.   Titem( scr_columns, scr_rows )
  98. {
  99.   state_word |= ( isSELECTED+isALIVE+isACCESSABLE );
  100.   desktop_x  = 0;  desktop_y  = 0;
  101.   desktop_xl = xl; desktop_yl = yl;
  102.   master_modal = NEW(Tmaster_modal);
  103.   put_in( master_modal, 0, 0 );
  104.   hook_idle( app_idle );
  105. }
  106.  
  107. void Tapplication::resize( int newxl, int newyl )
  108. {
  109.   int old_xl, old_yl;
  110.  
  111.   desktop_x  = 0;     desktop_y  = 0;
  112.   desktop_xl = newxl; desktop_yl = newyl;
  113.   old_xl = xl; old_yl = yl;
  114.   Titem::resize( newxl, newyl );
  115.   master_modal->resize( 0, 0 );
  116.   screen_dc->set_region( 0, 0, old_xl, old_yl );
  117.   screen_dc->validate();
  118.   redraw();
  119. }
  120.  
  121. static uint m;
  122. static void all_items_or( Titem *group )
  123. {
  124.   Titem *p;
  125.  
  126.   m |= group->event_mask;
  127.   p = group->first();
  128.   while( p != NULL )
  129.   {
  130.     all_items_or( p );
  131.     p = p->nextl();
  132.   }
  133. }
  134.  
  135. void Tapplication::update_events_mask( void )
  136. {
  137.   if( application != NULL )
  138.   {
  139.     m = 0;
  140.     all_items_or( this );
  141.     set_system_events_mask( m );
  142.   }
  143. }
  144.  
  145. void Tapplication::or_events_mask( Titem *p )
  146. {
  147.   set_system_events_mask( get_system_events_mask() | p->event_mask );
  148. }
  149.  
  150. void Tapplication::refresh_screen( void )
  151. {
  152.   int xx, yy;
  153.   Tdc *bufdc;
  154.  
  155.   if( xl!=scr_columns || yl!=scr_rows ) resize( scr_columns, scr_rows );
  156.   xx = screen_dc->region_xl; yy = screen_dc->region_yl;
  157.   if( ( xx <= 0 ) || ( yy <= 0 ) ) return;
  158.   bufdc = NEW( Tdc( xx, yy ) );
  159.   bufdc->x = screen_dc->region_x;
  160.   bufdc->y = screen_dc->region_y;
  161. //text_attr = 0xCF; bufdc->fill_dc( ' ' ); bufdc->draw(); delay( 10 );
  162.   set_dc( bufdc );
  163.   paint();
  164.   set_dc( screen_dc );
  165.   bufdc->draw();
  166.   DELETE( bufdc );
  167.   screen_dc->validate();
  168. }
  169.  
  170. void Tapplication::update_cursor( void )
  171. {
  172.   Titem *p, *l, *t;
  173.   int cursor_type;
  174.   int cursor_x, cursor_y, cx, cy;
  175.  
  176.   p = modal_item;
  177.   if( ( p == NULL ) || ( cursor_hides != 0 ) ) goto hide;
  178.   while( p->current != NULL ) p = p->current;
  179.   cursor_type = p->curs_type;
  180.   cursor_x = p->curs_x;
  181.   cursor_y = p->curs_y;
  182.   if( ( cursor_type == 0 ) ||
  183.       ( cursor_x < 0 ) || ( cursor_x >= p->xl ) ||
  184.       ( cursor_y < 0 ) || ( cursor_y >= p->yl ) ) goto hide;
  185.   while( p->owner != NULL )
  186.   {
  187.     if( !p->state( isALIVE ) ) goto hide;
  188.     cursor_x += p->x;
  189.     cursor_y += p->y;
  190.     t = p;
  191.     l = p = p->owner->last;
  192.     do
  193.     {
  194.       p = p->next; if( !p->state( isALIVE ) ) continue;
  195.       if( p == t ) break;
  196.       cx = cursor_x - p->x; cy = cursor_y - p->y;
  197.       if( ( cx >= 0 ) && ( cx < p->xl ) &&
  198.           ( cy >= 0 ) && ( cy < p->yl ) ) goto hide;
  199.     }
  200.     while( p != l );
  201.     p = p->owner;
  202.   }
  203.   if( cursor_type == 1 )
  204.     set_cursor( insert_cursor );
  205.   else
  206.     set_cursor( over_cursor );
  207.   goto update;
  208. hide:
  209.   set_cursor( no_cursor );
  210. update:
  211.   screen_dc->set_base( cursor_x, cursor_y );
  212.   screen_dc->update_cursor();
  213. }
  214.  
  215. //Tapplication protected
  216.  
  217. void Tapplication::event_handler( Tevent &ev )
  218. {
  219.   if( first() != master_modal ) pop_item( master_modal );
  220.   if( ev.code != evCOMMAND )
  221.     Titem::event_handler( ev );
  222.   else
  223.   {
  224.     switch( ev.CMD_CODE )
  225.     {
  226.       case cmAPP_QUIT:
  227.         stop(cmAPP_QUIT);
  228.         break;
  229.       case cmAPP_REDRAW:
  230. #if !defined( NOICONS ) && !defined( HGR )
  231.         restore_graph_chars();
  232.         set_graph_chars();
  233. #endif
  234.         redraw();
  235.         break;
  236.       default:
  237.         if( _cmd_handler != NULL ) _cmd_handler( ev.CMD_CODE, ev.CMD_INFO );
  238.         return;
  239.     }
  240.     handled( ev );
  241.   }
  242. }
  243.  
  244. void Tapplication::get_event( Tevent &ev )
  245. {
  246.   union REGS r;
  247.  
  248.   loop:
  249.     Titem::get_event( ev );
  250.     if( ev.code == evNOTHING )
  251.     {
  252.       idle( ev.TIME_PASSED );
  253.       refresh_screen();
  254.       INTR( 0x28, &r, &r );
  255.       goto loop;
  256.     }
  257.     master_modal->handle_event( ev );
  258.     if( ev.code == evNOTHING ) goto loop;
  259. }
  260.  
  261. //Tmaster_modal private
  262.  
  263. Tmaster_modal::Tmaster_modal( void ):
  264.   Titem( 0, 0 )
  265. {
  266.   set_events_mask( ~evCOMMAND, 0 );
  267.   set_state( isON_TOP, 1 );
  268.   set_flags( ifSELECTABLE, 0 );
  269.   set_flags( ifPRE_PROCESS, 1 );
  270. }
  271.  
  272. //PVapp publics:
  273.  
  274. void commands_changed( void )
  275. {
  276.   old_commands = (Tcommands *) MKFP( 0, 1 );
  277. }
  278.  
  279. static int dialog_x = xCENTER;
  280. static int dialog_y = yCENTER;
  281.  
  282. void _dialog_xy( int x, int y )
  283. {
  284.   dialog_x = x;
  285.   dialog_y = y;
  286. }
  287.  
  288. void __dialog_xy( int &x, int &y )
  289. {
  290.   x = dialog_x;
  291.   y = dialog_y;
  292.   dialog_x = xCENTER;
  293.   dialog_y = yCENTER;
  294. }
  295.  
  296. uint exec_dialog( Titem *p )
  297. {
  298.   int dx, dy;
  299.   uint result;
  300.  
  301.   if( ( application != NULL ) && is_valid( p ) )
  302.   {
  303.     __dialog_xy( dx, dy );
  304.     if( dx == xCENTER ) dx = ( desktop_xl - p->xl ) >> 1;
  305.     if( dy == yCENTER ) dy = ( desktop_yl - p->yl ) >> 1;
  306.     result = application->exec_item( p, desktop_x + dx, desktop_y + dy );
  307.     application->refresh_screen();
  308.     return result;
  309.   }
  310.   return cmCANCEL;
  311. }
  312.  
  313. Tidle hook_idle( Tidle p )
  314. {
  315.   Tidle result;
  316.  
  317.   result = idle;
  318.   idle = p;
  319.   return result;
  320. }
  321.  
  322. void cmd_handler( Tcmd_handler p )
  323. {
  324.   _cmd_handler = p;
  325. }
  326.  
  327. //STARTUP FUNCTIONS
  328.  
  329. static void tini_application( void )
  330. {
  331.   if( application != NULL ) DELETE( application );
  332.   application = NULL;
  333. }
  334.  
  335. void __init_application( void )
  336. {
  337.   application = NEW( Tapplication );
  338.   atexit( tini_application );
  339. }
  340.  
  341. void __resize_application( void )
  342. {
  343.   application->resize( application->xl, application->yl );
  344. }
  345.